﻿package com.sgcc.nfc.lock.presentation.logs

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.ItemLogBinding
import com.sgcc.nfc.lock.domain.model.OperationLog

class LogsAdapter : ListAdapter<OperationLog, LogsAdapter.LogViewHolder>(diffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogViewHolder {
        val binding = ItemLogBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return LogViewHolder(binding)
    }

    override fun onBindViewHolder(holder: LogViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    class LogViewHolder(private val binding: ItemLogBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: OperationLog) {
            val context = binding.root.context
            binding.logTypeText.text = context.getString(
                R.string.log_operation_type_format,
                context.getString(operationTypeLabel(item.operationType))
            )
            binding.logUserText.text = context.getString(
                R.string.log_operation_user_format,
                item.userName?.takeIf { it.isNotBlank() } ?: context.getString(R.string.common_value_unknown)
            )
            binding.logRoomText.text = buildString {
                append(
                    context.getString(
                        R.string.log_operation_room_format,
                        item.roomName?.takeIf { it.isNotBlank() }
                            ?: context.getString(R.string.common_value_unknown)
                    )
                )
                if (!item.lockNfcId.isNullOrBlank()) {
                    appendLine()
                    append(
                        context.getString(
                            R.string.log_operation_lock_format,
                            item.lockNfcId
                        )
                    )
                }
            }
            binding.logTimeText.text = context.getString(
                R.string.log_operation_time_format,
                item.operationTime?.takeIf { it.isNotBlank() }
                    ?: context.getString(R.string.common_value_unknown)
            )
        }
    }

    companion object {
        private val diffCallback = object : DiffUtil.ItemCallback<OperationLog>() {
            override fun areItemsTheSame(oldItem: OperationLog, newItem: OperationLog): Boolean = oldItem.logId == newItem.logId
            override fun areContentsTheSame(oldItem: OperationLog, newItem: OperationLog): Boolean = oldItem == newItem
        }

        private fun operationTypeLabel(type: Int): Int = when (type) {
            1 -> R.string.log_operation_type_add
            2 -> R.string.log_operation_type_open
            3 -> R.string.log_operation_type_close
            else -> R.string.log_operation_type_unknown
        }
    }
}
